import { _decorator, Camera, Component, director, find, game, instantiate, Label, log, Node, PlaneCollider, Prefab, resources, RigidBody, Sprite, v3, Vec3 } from 'cc';
import { GameManager } from './GameManager';
import { PlayerController } from './PlayerController';
import { Monster } from '../Monster';
import { Player } from '../Player';
import { UIManager } from './UIManager';
import { AudioManager } from './AudioManager';

const { ccclass, property } = _decorator;

const v3_hitpos = v3();

@ccclass('AniController')
export class AniController extends Component {

    private static _instance: AniController = null;
    public static get Instance(): AniController {
        if (!this._instance) {
            this._instance = new AniController();
        }
        return this._instance;
    }

    BossIsAtk: boolean = false;

    protected onLoad(): void {
        if (AniController._instance == null) {
            AniController._instance = this;
        }
    }

    // start() {

    // }

    // update(deltaTime: number) {

    // }

    MonsterJumpDownEvent(params) {//怪物落地动画帧事件
        if (!GameManager.Instance.IsEnterFight) {
            resources.load("Prefabs/FallRender", Prefab, (err, pre) => {
                let fall = instantiate(pre);
                fall.setParent(director.getScene());
                fall.setWorldPosition(this.node.parent.getWorldPosition().x, fall.position.y, this.node.parent.getWorldPosition().z);
                this.scheduleOnce(() => {
                    fall.destroy();
                }, 2)
            })
            GameManager.Instance.Shaking(PlayerController.Instance.v3_TargetCameraPos, 0.05, 0.5);
        }
        else {
            if (GameManager.Instance.GetLocalStorage("Level") == 4 || GameManager.Instance.GetLocalStorage("Level") == 9 || GameManager.Instance.GetLocalStorage("Level") == 14) {
                if (this.node.parent.getComponent(Monster).IsEnmey) {
                    PlayerController.Instance.CameraPosed = PlayerController.Instance.v3_TargetCameraPos;
                    GameManager.Instance.Shaking(PlayerController.Instance.v3_TargetCameraPos, 0.05, 0.5);
                }
            }
        }
    }

    MonsterAttackEvent(params) {//怪物攻击动画帧事件
        let monsterScript = this.node.parent.getComponent(Monster);
        if (GameManager.Instance.GetLocalStorage("Level") == 4 || GameManager.Instance.GetLocalStorage("Level") == 9 || GameManager.Instance.GetLocalStorage("Level") == 14) {
            if (monsterScript.IsEnmey) {
                let playerPower: number = 0;
                for (let i in GameManager.Instance.PlayerMonsters) {
                    playerPower += GameManager.Instance.PlayerMonsters[i].getComponent(Monster).PowerValue;
                }
                if (monsterScript.PowerValue <= playerPower) {
                    monsterScript.MonsterAni.crossFade("Dead");
                    AudioManager.Instance.playSound("Audio/Hit");
                    this.scheduleOnce(() => {
                        UIManager.Instance.ShowWinPanel();
                    }, 1)
                }
                else {
                    AudioManager.Instance.playSound("Audio/Hit");
                    for (let i in GameManager.Instance.PlayerMonsters) {
                        GameManager.Instance.PlayerMonsters[i].getComponent(Monster).MonsterAni.crossFade("Dead");
                    }
                    this.scheduleOnce(() => {
                        UIManager.Instance.ShowFailedPanel();
                    }, 1)
                }
            }
        }
        else {
            if (monsterScript.IsEnmey) {
                AudioManager.Instance.playSound("Audio/Hit");
                let playerMonster = GameManager.Instance.PlayerMonsters[GameManager.Instance.EnemyMonsters.indexOf(this.node.parent)];
                if (monsterScript.PowerValue <= playerMonster.getComponent(Monster).PowerValue) {
                    monsterScript.MonsterAni.crossFade("Dead");
                    GameManager.Instance.EnemyDeadNum++;
                    if (GameManager.Instance.GetLocalStorage("Level") == 0) {
                        if (GameManager.Instance.EnemyDeadNum >= 1) {
                            this.scheduleOnce(() => {
                                UIManager.Instance.ShowWinPanel();
                            }, 1)
                        }
                    }
                    else {
                        if (GameManager.Instance.EnemyDeadNum >= 2) {
                            this.scheduleOnce(() => {
                                UIManager.Instance.ShowWinPanel();
                            }, 1)
                        }
                    }
                }
            }
            else {
                let enemyMonster = GameManager.Instance.EnemyMonsters[GameManager.Instance.PlayerMonsters.indexOf(this.node.parent)];
                if (monsterScript.PowerValue <= enemyMonster.getComponent(Monster).PowerValue) {
                    monsterScript.MonsterAni.crossFade("Dead");
                    GameManager.Instance.PlayerDeadNum++;
                    if (GameManager.Instance.GetLocalStorage("Level") == 0) {
                        if (GameManager.Instance.PlayerDeadNum >= 1) {
                            this.scheduleOnce(() => {
                                UIManager.Instance.ShowFailedPanel();
                            }, 1)
                        }
                    }
                    else {
                        if (GameManager.Instance.PlayerDeadNum >= 2) {
                            this.scheduleOnce(() => {
                                UIManager.Instance.ShowFailedPanel();
                            }, 1)
                        }
                    }
                }
            }
        }
    }

    PlayerBackEvent(params) {//玩家后退动作帧事件
        this.node.parent.getComponent(Player).PlayerRigibody.setLinearVelocity(Vec3.ZERO);
    }
}


